<?php

require_once("GenericWriter.php");

class FetchWriter extends GenericWriter{
	
	public function __construct($object = NULL, $connection = NULL){
		parent::__construct($object, $connection);
	}
	
	public function save(){
		if(!is_null($this->object) && is_a($this->object, "Fetch")){
			$insert = new RXInsert($this->connection);
			$insert->addTable("fetchs");
			$insert->addFieldValue("location", urlencode($this->object->getLocation()));
			$insert->addFieldValue("scheduled_date", time());
			$insert->addFieldValue("last_change", time());
			$insert->addFieldValue("status", 0);
			$insert->addFieldValue("ttl", $this->object->getTtl());
			$insert->addFieldValue("content", "");
			$insert->addFieldValue("title", "");
			$insert->addFieldValue("keywords", "");
			$insert->addFieldValue("paragraphs", "");
			$insert->addFieldValue("description", "");
			$insert->addFieldValue("link_title", utf8_encode(htmlentities($this->object->getLinkTitle(), ENT_QUOTES)));
			$insert->addFieldValue("link_text", utf8_encode(htmlentities($this->object->getLinkText(), ENT_QUOTES)));
			$insert->setIgnore();
			if($insert->execute()){				
				return $insert->getLastInsertedId();
			}else{
				return FALSE;
			}		
		}else{
			return FALSE;
		}
	}
	
	public function delete(){	
		if(!is_null($this->object) && is_a($this->object, "Fetch")){
			$delete = new RXDelete($this->connection); 
			$delete->addTable("fetchs");
			$delete->addWhereAndStatementEquals("id", $this->object->getId());			
			return $delete->execute();		
		}else{
			return FALSE;
		}
	}
	
	public function update(){	
		if(!is_null($this->object) && is_a($this->object, "Fetch")){
			$update = new RXUpdate($this->connection);

			switch($this->operation){
				case "BLOCK_FETCHER": 
								$update->addTable("fetchs");
								$update->addField("status", "1");
								$update->addField("last_change", time());
								$update->addWhereAndStatementEquals("id", $this->object->getId());
								break;
				case "FINISH_FETCHER":								
								$update->addTable("fetchs");
								$update->addField("status", "2");
								$update->addField("last_change", time());
								$update->addField("content", utf8_encode(htmlentities($this->object->getContent(), ENT_QUOTES)));
								$update->addWhereAndStatementEquals("id", $this->object->getId());
								break;
				case "BLOCK_PROCESSER":
								$update->addTable("fetchs");
								$update->addField("status", "3");
								$update->addField("last_change", time());
								$update->addWhereAndStatementEquals("id", $this->object->getId());
								break;
				case "FINISH_PROCESSER":								
								$update->addTable("fetchs");
								$update->addField("status", "4");
								$update->addField("last_change", time());
								$update->addField("keywords", utf8_encode(htmlentities($this->object->getKeywords(), ENT_QUOTES)));
								$update->addField("title", utf8_encode(htmlentities($this->object->getTitle(), ENT_QUOTES)));
								$update->addField("description", utf8_encode(htmlentities($this->object->getDescription(), ENT_QUOTES)));
								$update->addField("paragraphs", utf8_encode(htmlentities($this->object->getKeywords(), ENT_QUOTES)));
								$update->addField("content", "");
								$update->addWhereAndStatementEquals("id", $this->object->getId());
								break;
				case "BLOCK_SCORER": 								
								$update->addTable("fetchs");
								$update->addField("status", "5");
								$update->addField("last_change", time());
								$update->addWhereAndStatementEquals("id", $this->object->getId());
								break;
				default:	FALSE;
			}
						
			return $update->execute();
			
		}
	}	
}